package com.lvtongka.app.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.lvtongka.app.R
import com.lvtongka.app.base.BaseVBActivity
import com.lvtongka.app.common.LocationUtils
import com.lvtongka.app.databinding.ActivityHomeBinding
import com.lvtongka.app.fragment.home.HomeFragment
import com.lvtongka.app.fragment.my.MyFragment
import com.lvtongka.app.ui.qr.PaymentCodeActivity

open class HomeActivity : BaseVBActivity<ActivityHomeBinding>() {

    companion object {
        const val TAG = "HomeActivity"
    }

    private var mExitTime: Long = 0
    private var currentFragment: Fragment? = null
    private val homeFragment = HomeFragment()
    private val myFragment = MyFragment()
    private var oldPosition = 0

    private var normalTextColor = Color.parseColor("#757373")
    private var normalIcon = intArrayOf(
        R.mipmap.icon_tab_home_un,
        R.mipmap.icon_tab_my_un
    )
    private var selectTextColor = Color.parseColor("#e31f1f")

    private var selectIcon = intArrayOf(
        R.mipmap.icon_tab_home,
        R.mipmap.icon_tab_my
    )

    override fun initBinding(): ActivityHomeBinding {
        return ActivityHomeBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        switchFragment(homeFragment)

        binding.first.setOnClickListener {
            if (oldPosition != 0) {
                setUnSelect()
                setSelect(0)
            }
        }
        binding.four.setOnClickListener {
            if (oldPosition != 1) {
                setUnSelect()
                setSelect(1)
            }
        }
        binding.ivScan.setOnClickListener {
            startActivityForResult(Intent(this@HomeActivity, PaymentCodeActivity::class.java), 25)
        }
    }

    //切换fragment
    private fun switchFragment(targetFragment: Fragment) {
        val transaction = supportFragmentManager
            .beginTransaction()
        if (!targetFragment.isAdded) {
            if (currentFragment == null) {
                transaction
                    .add(R.id.main_fragment, targetFragment)
                    .commit()
            } else {
                transaction
                    .hide(currentFragment!!)
                    .add(R.id.main_fragment, targetFragment)
                    .commit()
            }
        } else {
            transaction
                .hide(currentFragment!!)
                .show(targetFragment)
                .commit()
            if (targetFragment is MyFragment) {
                targetFragment.refresh()
            }
        }
        currentFragment = targetFragment
        /*
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.main_fragment, targetFragment)
        transaction.commitAllowingStateLoss()
         */
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 25) {
            if (currentFragment is MyFragment) {
                (currentFragment as MyFragment).refresh()
            }
        }
    }

    open fun setUnSelect() {
        when (oldPosition) {
            0 -> {
                binding.ivFirst.setImageResource(normalIcon[0])
                binding.tvFirst.setTextColor(normalTextColor)
            }
            1 -> {
                binding.ivFour.setImageResource(normalIcon[1])
                binding.tvFour.setTextColor(normalTextColor)
            }
        }
    }

    open fun setSelect(position: Int) {
        oldPosition = position
        when (position) {
            0 -> {
                binding.ivFirst.setImageResource(selectIcon[0])
                binding.tvFirst.setTextColor(selectTextColor)
                switchFragment(homeFragment)
            }
            1 -> {
                binding.ivFour.setImageResource(/* resId = */ selectIcon[1])
                binding.tvFour.setTextColor(selectTextColor)
                switchFragment(myFragment)
            }
        }
    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            showToast("再按一次退出系统")
            mExitTime = System.currentTimeMillis()
        } else {
            finish()
        }
    }

    override fun onDestroy() {
        LocationUtils.getInstance(this).removeLocationUpdatesListener()
        super.onDestroy()
    }

    override fun initEvent() {

    }

    override fun initData() {
    }
}